package com.kexilo.system.system.controller;

import com.kexilo.core.common.annotation.Debounce;
import com.kexilo.core.common.annotation.Idempotent;
import com.kexilo.core.common.annotation.Log;
import com.kexilo.core.common.core.page.TableDataInfo;
import com.kexilo.core.common.core.web.BaseController;
import com.kexilo.core.common.core.web.domain.R;
import com.kexilo.core.common.enums.BusinessType;
import com.kexilo.core.common.utils.SecurityUtils;
import com.kexilo.core.common.utils.StringUtils;
import com.kexilo.core.system.domain.SysDictData;
import com.kexilo.system.system.service.ISysDictDataService;
import com.kexilo.system.system.service.ISysDictTypeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 数据字典信息
 * 
 * @author Kexilo
 */
@RestController
@RequestMapping("/system/dict/data")
@Tag(name = "字典数据管理", description = "字典数据信息管理相关接口")
public class SysDictDataController extends BaseController {

    private static final Logger log = LoggerFactory.getLogger(SysDictDataController.class);
    
    @Autowired
    private ISysDictDataService dictDataService;

    @Autowired
    private ISysDictTypeService dictTypeService;

    @PreAuthorize("@ss.hasPermi('system:dict:list')")
    @GetMapping("/list")
    @Operation(summary = "获取字典数据列表", description = "获取字典数据列表信息")
    @ApiResponses({
        @ApiResponse(responseCode = "200", description = "获取成功"),
        @ApiResponse(responseCode = "403", description = "权限不足")
    })
    public TableDataInfo list(SysDictData dictData) {
        startPage();
        List<SysDictData> list = dictDataService.selectDictDataList(dictData);
        return getDataTable(list);
    }

    @Debounce(interval = 10, message = "导出操作过于频繁，请稍后再试")
    @Log(title = "字典数据", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('system:dict:export')")
    @PostMapping("/export")
    @Operation(summary = "导出字典数据", description = "导出字典数据")
    public R<Void> export(SysDictData dictData) {
        List<SysDictData> list = dictDataService.selectDictDataList(dictData);
        // TODO: 实现导出功能
        return success();
    }

    /**
     * 查询字典数据详细
     */
    @PreAuthorize("@ss.hasPermi('system:dict:query')")
    @GetMapping(value = "/{dictCode}")
    @Operation(summary = "获取字典数据详情", description = "根据字典数据ID获取详细信息")
    public R<SysDictData> getInfo(@Parameter(description = "字典数据ID") @PathVariable Long dictCode) {
        return success(dictDataService.selectDictDataById(dictCode));
    }

    /**
     * 根据字典类型查询字典数据信息
     */
    @GetMapping(value = "/type/{dictType}")
    @Operation(summary = "根据字典类型查询字典数据", description = "根据字典类型获取字典数据列表")
    public R<List<SysDictData>> dictType(@Parameter(description = "字典类型") @PathVariable String dictType) {
        List<SysDictData> data = dictTypeService.selectDictTypeByType(dictType) != null 
            ? dictDataService.selectDictDataByType(dictType) 
            : new ArrayList<SysDictData>();
        return success(data);
    }

    /**
     * 新增字典数据
     */
    @PreAuthorize("@ss.hasPermi('system:dict:add')")
    @Log(title = "字典数据", businessType = BusinessType.INSERT)
    @Debounce(strategy = Debounce.Strategy.REQUEST_PARAMS, interval = 5, message = "请勿重复提交字典数据信息")
    @Idempotent(strategy = Idempotent.Strategy.REQUEST_PARAMS, key = "#dictData.dictType + ':' + #dictData.dictValue", timeout = 5, message = "该字典数据正在处理中，请稍后再试")
    @PostMapping
    @Operation(summary = "新增字典数据", description = "新增字典数据信息")
    public R<Void> add(@Validated @RequestBody SysDictData dictData) {
        dictData.setCreateBy(SecurityUtils.getUsername());
        return toAjax(dictDataService.insertDictData(dictData));
    }

    /**
     * 修改保存字典数据
     */
    @PreAuthorize("@ss.hasPermi('system:dict:edit')")
    @Log(title = "字典数据", businessType = BusinessType.UPDATE)
    @Debounce(strategy = Debounce.Strategy.REQUEST_PARAMS, interval = 3, message = "请勿频繁修改字典数据信息")
    @Idempotent(strategy = Idempotent.Strategy.REQUEST_PARAMS, key = "#dictData.dictCode", timeout = 3, message = "字典数据信息正在更新中，请稍后再试")
    @PutMapping
    @Operation(summary = "修改字典数据", description = "修改字典数据信息")
    public R<Void> edit(@Validated @RequestBody SysDictData dictData) {
        dictData.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(dictDataService.updateDictData(dictData));
    }

    /**
     * 删除字典数据
     */
    @PreAuthorize("@ss.hasPermi('system:dict:remove')")
    @Log(title = "字典数据", businessType = BusinessType.DELETE)
    @Debounce(strategy = Debounce.Strategy.USER_METHOD, interval = 5, message = "删除操作过于频繁，请稍后再试")
    @Idempotent(strategy = Idempotent.Strategy.REQUEST_PARAMS, timeout = 5, delKey = true, message = "删除操作正在进行中，请勿重复操作")
    @DeleteMapping("/{dictCodes}")
    @Operation(summary = "删除字典数据", description = "删除字典数据信息")
    public R<Void> remove(@Parameter(description = "字典数据ID数组") @PathVariable Long[] dictCodes) {
        dictDataService.deleteDictDataByIds(dictCodes);
        return success();
    }
}
